/*
visqrt
Copyright (C) 2010  Bhupendra Aole

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/
/*
 * File: VQApplication.java
 * Author: Bhupendra Aole
 * Date: Sep 8, 2010: 
 * 
 * Revisions:
 * ----------
 */

package aole.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.script.ScriptException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

import aole.VQMain;

public class VQApplication extends JFrame {
	private JTextArea txtScript;

	public VQApplication() {
		super("Vision Quest - SDL");
	}

	public void display () {
		createGUI();
		pack();
		setVisible(true);
	}

	public void createGUI () {

		String s = "var s = new Sphere(2);\nscene.addRenderable(s);\n"
				+ "scene.render();";
		txtScript = new JTextArea(s, 20, 80);
		add(txtScript, BorderLayout.CENTER);
		JButton btnEval = new JButton("Evaluate");
		btnEval.setActionCommand("eval");
		btnEval.addActionListener(new EvalActionListener());
		add(btnEval, BorderLayout.SOUTH);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing (WindowEvent e) {
				super.windowClosing(e);
				System.exit(0);
			}
		});
	}

	public VQRenderScreen getRenderScreen (int width, int height) {
		return new VQRenderScreen(this, width, height);
	}

	// inner classes
	class EvalActionListener implements ActionListener {
		public void actionPerformed (ActionEvent e) {
			if (e.getActionCommand().equals("eval")) {
				try {
					VQMain.getScene().init();
					VQMain.getEngine().eval(txtScript.getText());
				} catch (ScriptException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
}
